/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SanBay.java
 *
 * Created on May 27, 2011, 10:19:21 AM
 */
package gui;

import dao.SanBayDAO;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.SanBayPOJO;

/**
 *
 * @author b2
 */
public class SanBayFrm extends javax.swing.JFrame {

    /** Creates new form SanBay */
    public SanBayFrm() {
        initComponents();
    }

    public void hienThi() {
        ArrayList<SanBayPOJO> ds = SanBayDAO.LayDanhSachSanBay();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã sân bay");
        dtm.addColumn("Tên sân bay");

        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            SanBayPOJO sb = ds.get(i);
            dtm.setValueAt(sb.getMaSanBay(), i, 0);
            dtm.setValueAt(sb.getTenSanBay(), i, 1);
        }
        this.jtbSanBay.setModel(dtm);
        this.jtbSanBay.changeSelection(0, 0, false, false);

    }

    public void tiemKiemSanBay() {
        String maSanBay = this.txtTimKiemSanBay.getText();
        ArrayList<SanBayPOJO> ds = SanBayDAO.TimKiemSanBayTheoTen(maSanBay);
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã sân bay");
        dtm.addColumn("Tên sân bay");

        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            SanBayPOJO sb = ds.get(i);
            dtm.setValueAt(sb.getMaSanBay(), i, 0);
            dtm.setValueAt(sb.getTenSanBay(), i, 1);
        }
        this.jtbSanBay.setModel(dtm);
        this.jtbSanBay.changeSelection(0, 0, false, false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbSanBay = new javax.swing.JTable();
        btnCapNhat = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnThemSanBay = new javax.swing.JButton();
        btnXoaSanBay = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtTimKiemSanBay = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản lý sân bay");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("QUẢN LÝ SÂN BAY");

        jtbSanBay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbSanBay);

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnThemSanBay.setText("Thêm");
        btnThemSanBay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemSanBayActionPerformed(evt);
            }
        });

        btnXoaSanBay.setText("Xóa");
        btnXoaSanBay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaSanBayActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(btnThemSanBay, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoaSanBay, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCapNhat)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThemSanBay)
                    .addComponent(btnXoaSanBay)
                    .addComponent(btnCapNhat)
                    .addComponent(btnThoat))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jLabel2.setText("Tên sân bay");

        txtTimKiemSanBay.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtTimKiemSanBayCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jLabel2)
                        .addGap(54, 54, 54)
                        .addComponent(txtTimKiemSanBay, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTimKiemSanBay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.hienThi();
    }//GEN-LAST:event_formWindowOpened

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        // TODO add your handling code here:
        this.hienThi();
    }//GEN-LAST:event_btnCapNhatActionPerformed

    private void btnThemSanBayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemSanBayActionPerformed
        // TODO add your handling code here:
        ThemSanBayFrm frm = new ThemSanBayFrm();
        frm.setVisible(true);
    }//GEN-LAST:event_btnThemSanBayActionPerformed

    private void txtTimKiemSanBayCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtTimKiemSanBayCaretUpdate
        // TODO add your handling code here:
        this.tiemKiemSanBay();
    }//GEN-LAST:event_txtTimKiemSanBayCaretUpdate

    //XÓA
    public void xoaSanBay() {
        int dong = this.jtbSanBay.getSelectedRow();
        String maSanBay = (String) this.jtbSanBay.getValueAt(dong, 0);
        boolean kq = SanBayDAO.xoaSanBay(maSanBay);
        if (kq == true) {
            JOptionPane.showMessageDialog(this, "Xóa thành công");
            //this.TimKiem();
            this.tiemKiemSanBay();
        } else {
            JOptionPane.showMessageDialog(this, "Xóa không thành công");
        }

    }
    private void btnXoaSanBayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaSanBayActionPerformed
        // TODO add your handling code here:
        int dong = this.jtbSanBay.getSelectedRow();
        if (dong >= 0) {
            int kq = JOptionPane.showConfirmDialog(this, "Bạn có muỗn xóa không", "Thông báo", JOptionPane.YES_NO_OPTION);
            if (kq == JOptionPane.YES_OPTION) {
                this.xoaSanBay();
            }
        }
    }//GEN-LAST:event_btnXoaSanBayActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SanBayFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JButton btnThemSanBay;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoaSanBay;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbSanBay;
    private javax.swing.JTextField txtTimKiemSanBay;
    // End of variables declaration//GEN-END:variables
}
